﻿using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ChatClient.Tools.Converters
{
    public class ByteBitmapSourceConverter : IValueConverter
    {
        public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(ImageSource))
            {
                throw new InvalidOperationException("The target must be a ImageSource");
            }

            if (value is not byte[] img || img.Length == 0)
            {
                return null;
            }

            var converter = new ImageSourceConverter();
            var bmpSrc = (BitmapSource?)converter.ConvertFrom(img);
            return bmpSrc;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}